package com.anonymous.community.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * 状态栏工具类
 * 用于处理状态栏适配相关功能
 */
public class StatusBarUtil {

    /**
     * 获取状态栏高度
     * @param context 上下文
     * @return 状态栏高度（像素）
     */
    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        // 默认状态栏高度（适用于大多数设备）
        return (int) (24 * resources.getDisplayMetrics().density);
    }

    /**
     * 设置状态栏透明
     * @param activity Activity实例
     */
    public static void setTransparentStatusBar(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            );
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(android.graphics.Color.TRANSPARENT);
        }
    }

    /**
     * 设置状态栏颜色和文字颜色
     * @param activity Activity实例
     * @param color 状态栏颜色
     * @param darkText 是否使用深色文字
     */
    public static void setStatusBarColor(Activity activity, int color, boolean darkText) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(color);
            
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && darkText) {
                window.getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                );
            }
        }
    }

    /**
     * 为指定的View设置状态栏高度
     * @param context 上下文
     * @param view 需要设置高度的View
     */
    public static void setStatusBarHeight(Context context, View view) {
        if (view != null) {
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.height = getStatusBarHeight(context);
            view.setLayoutParams(params);
        }
    }

    /**
     * 为Activity设置沉浸式状态栏
     * @param activity Activity实例
     * @param statusBarView 状态栏占位View
     */
    public static void setupImmersiveStatusBar(Activity activity, View statusBarView) {
        // 设置状态栏颜色为白色，文字为深色
        setStatusBarColor(activity, android.graphics.Color.WHITE, true);
        
        // 设置状态栏占位View的高度
        setStatusBarHeight(activity, statusBarView);
    }
}